using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using MyProject.Mqtt;
using MyProject.Util.Helper;
using MyProject.Util.Model;
using System.IO;

namespace MyProject.Web
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            // 读取配置
            services.AddConfigurableOptions<SystemConfig>();
        }

        public async void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            DefaultFilesOptions defaultFilesOptions = new DefaultFilesOptions();
            defaultFilesOptions.DefaultFileNames.Clear();
            defaultFilesOptions.DefaultFileNames.Add("index.html");
            app.UseDefaultFiles(defaultFilesOptions);

            // 注册wwwroot静态文件访问
            app.UseStaticFiles();

            // 注册Resource静态文件访问
            string resource = Path.Combine(env.ContentRootPath, "Resource");
            FileHelper.CreateDirectory(resource);
            app.UseStaticFiles(new StaticFileOptions
            {
                RequestPath = "/Resource",
                FileProvider = new PhysicalFileProvider(resource),
            });

            // MQTT客户端服务，注意，这里是强制同步执行了
            await MqttClientCenter.OpenConnectAsync();
        }
    }
}